const express = require('express')
const mysql = require('mysql')
const multer = require('multer')
<<<<<<< HEAD
=======
const fs = require('fs')
>>>>>>> origin/master


const router = express.Router()




const pool = mysql.createPool({
<<<<<<< HEAD
    connectionLimit:10,
    host:"localhost",
    user:"root",
    password:"",
    database:"newcust"
})


router.get("/hello",(req,res)=>{

    const sql = 'select * from filedemo0403 where id'
    pool.query(sql,[],(error,result)=>{
        res.send(result)
        
        
    })
})



const storage = multer.diskStorage({
    destination: function (req, file, cb) {
        cb(null, 'uploads/') // 保存到服务器的目录
    },
    filename: function (req, file, cb) {
        cb(null, Date.now() + '-' + file.originalname) // 文件名处理
    }
})
const upload = multer({ storage: storage })

// 文件上传路由
router.post('/uploads', upload.single('file'), (req, res) => {
    console.log(req.file);
    
    if (!req.file) {
        return res.status(400).send('没有上传文件');
    }
    res.json({
        path: req.file.path,
        size: req.file.size
    });
});


=======
    connectionLimit: 10,
    host: "localhost",
    user: "root",
    password: "",
    database: "newcust"
})


router.get("/hello", (req, res) => {

    const sql = 'select * from filedemo0403 where id'
    pool.query(sql, [], (error, result) => {
        res.send(result)


    })
})

// const storage = multer.diskStorage({
//     destination: function (req, file, cb) {
//         cb(null, 'uploads/') // 保存到服务器的目录
//     },
//     filename: function (req, file, cb) {
//         cb(null, Date.now() + '-' + file.originalname) // 文件名处理
//     }
// })
// const upload = multer({ storage: storage })

// 文件上传路由
router.post('/uploads', (req, res) => {
    const file = req.files[0]
    
    if (!file) {
        return res.status(400).send('没有上传文件');
    }

    fs.rename('./uploads/' + file.filename,'./uploads/' +Date.now() + file.originalname,(err)=>{
        if(err){
            throw err;
        }
        res.send("上传成功！")
    } )
    
});




>>>>>>> origin/master
module.exports = router;